package net.melon.agency.inst.ssh.action;

import javax.annotation.Resource;

import net.esj.basic.core.engine.util.enumeration.EnumProvidersFactory;
import net.esj.basic.exception.MelonException;
import net.esj.basic.pojo.UpdateType;
import net.esj.basic.utils.DateUtils;
import net.esj.basic.utils.view.BaseJsonMessage.MessageLevel;
import net.esj.basic.view.AbstractPluginPageAction;
import net.melon.agency.inst.ssh.service.AgencyService;
import net.melon.agency.pojo.Agency;
import net.melon.agency.pojo.Agency.AgencyState;

public class AgencyAction extends AbstractPluginPageAction<Agency, Agency> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8386262237004803432L;

	@Resource
	private EnumProvidersFactory enumProvidersFactory;
	
	private int status;
	
	private String pid;
	
	@Resource
	private AgencyService agencyService;
	
	private String agency_name;
	private String orgin_name;
	private String parentName;

	@Override
	protected void doQueryPage() {
		pagination = agencyService.pageAgency(pagination,parentName);
	}

	
	protected void doUpdate() throws Exception{
		if(!agencyService.checkAgencyExist(entity)){
			super.doUpdate();
		}else{
			throw new MelonException("exist name");
		}
	}
	
	@Override
	protected void doGetEntity() {
		entity = agencyService.getAgency(id);
	}

	public String verify(){
		return SUCCESS;
	}

	public String select(){
		return SUCCESS;
	}

	@Override
	protected boolean beforeUpdate() {
		entity.setUpdateType(UpdateType.BY_DIFF);
		return super.beforeUpdate();
	}


	@Override
	protected boolean beforeAdd() {
		entity.setGmtCreate(DateUtils.now());
		return super.beforeAdd();
	}
	
	@Override
	protected void doAdd() throws Exception {
		agencyService.saveAgency(entity , getOperator());
	}

	public void state(){
		Agency age = defaultService.get(id, Agency.class);
		if(age!=null){
			age.setState(status);
			defaultService.update(age);
		}
		writeBaseJsonMessage("", getText(ACTION_SUCCESS), MessageLevel.INFO, "html");
	}

	@Override
	public void doDelete() {
	}

	public AgencyState[] getStates() {
		return AgencyState.values();
	}

	public Agency getEntity(){
		return entity;
	}
	
	public void setEntity(Agency entity){
		this.entity = entity;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}


	public String getAgency_name() {
		return agency_name;
	}


	public void setAgency_name(String agency_name) {
		this.agency_name = agency_name;
	}


	public String getOrgin_name() {
		return orgin_name;
	}


	public void setOrgin_name(String orgin_name) {
		this.orgin_name = orgin_name;
	}


	public String getParentName() {
		return parentName;
	}


	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	
	
}
